package com.capinfo.accounting.web;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.NativeWebRequest;

import com.capinfo.accumulation.model.accounting.mybist.GB_JZPZXX;
import com.capinfo.accumulation.model.accounting.mybist.HZJZPZ;
import com.capinfo.accumulation.model.pagemodel.Json;
import com.capinfo.accumulation.parameter.accounting.DataGrid;
import com.capinfo.accumulation.service.accounting.mybits.GB_JZPZXXServiceI;
import com.capinfo.accumulation.service.accounting.mybits.HZJZPZServiceI;
import com.capinfo.accumulation.service.accounting.mybits.XYRJZServiceI;
import com.capinfo.accumulation.util.JSONUtil;
import com.capinfo.commons.project.model.security.SystemUser;
import com.capinfo.components.security.web.utils.WebRequestUtils;

/**
 * 转账凭证控制器
 * @author 
 * 
 */
@Controller
@RequestMapping("/business/accounting/zzpz")
public class ZZPZController {
	
	//记账凭证
	@Autowired
	public GB_JZPZXXServiceI gB_JZPZXXService;
	
	//汇总凭证
	@Autowired
	HZJZPZServiceI hZJZPZService;
	
	@Autowired
	public XYRJZServiceI xYRJZServiceI;
	
	
	/**
	 * 转账凭证分录查询
	 * @param param
	 * @return
	 */
	@RequestMapping("/zzpzflGrid.shtml")
	@ResponseBody
	public DataGrid zzpzflGrid(@RequestParam  Map param) {
		return xYRJZServiceI.zzpzFlByGrid(param);
	}
	
	
	/**
	 * 添加分录保存
	 * @param t
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping("/bgadd.shtml")
	@ResponseBody
	public Json bgadd(GB_JZPZXX t,HttpSession session,NativeWebRequest request,String xspzh) {
		Json j = new Json();
		String entity=request.getParameter("inserted");
		List l=JSONUtil.toList(entity);
		 for (int i = 0; i < l.size(); i++) {
				Map map = (Map) l.get(i);
				try {
					t.setID(map.get("ID")==null||"".equals(map.get("ID"))?null:Long.parseLong((map.get("ID").toString()).replace(".0", "")));
					t.setHSDW(Long.parseLong(map.get("HSDW").toString().replace(".0", "")));
					t.setFJDJS(Long.parseLong(map.get("FJDJS").toString().replace(".0", "")));
					t.setZHAIYAO(map.get("ZHAIYAO").toString());
					t.setKMBH(map.get("KMBH").toString());
					try {
						t.setJFFSE(Double.parseDouble(map.get("JFFSE").toString()));
					} catch (Exception e) {}
					try {
						t.setDFFSE(Double.parseDouble(map.get("DFFSE").toString()));
					} catch (Exception e) {}
					t.setPZLB("2");
					t.setXSPZH(xspzh);
					
					if(t.getID()==null){
						gB_JZPZXXService.insert(t);
						j.setSuccess(true);
						j.setMsg("添加成功");
						j.setObj(t);
					}else{
						gB_JZPZXXService.updateByPrimaryKeySelective(t);	
						j.setSuccess(true);
						j.setMsg("修改成功");
						j.setObj(t);
					}
				} catch (Exception e) {
					e.printStackTrace();
					j.setMsg("操作失败");
				}
			}
		
		return j;
	}
	
	
	/**
	 * 跳转添加分录
	 * @return
	 */
	@RequestMapping("/tjfladd.shtml")
	public String tjfladd(String xspzh ,HttpServletRequest request) {
		GB_JZPZXX gb = new GB_JZPZXX();
//		gb.setXSPZH(xspzh);
		gb.setXSPZH("000");
		request.setAttribute("gb", gb);
		return "/business/accounting/zzpz/tjfladd";
	}
	
	
	/**
	 * 转账凭证删除
	 * @param id
	 * @return
	 */
	@RequestMapping("/pzfldelete.shtml")
	@ResponseBody
	public Json pzfldelete(String id) {
		Json j = new Json();
		try {
			gB_JZPZXXService.deleteByPrimaryKey(id);
			j.setSuccess(true);
			j.setMsg("删除成功");
		} catch (Exception e) {
			 e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}
	
	
	/**
	 * 转账凭证编辑保存
	 * @param t
	 * @param session
	 * @param request
	 * @return
	 * @throws ParseException 
	 */
	@RequestMapping("/edit.shtml")
	@ResponseBody
	public Json edit(HZJZPZ t , HttpSession session,NativeWebRequest request) throws ParseException {
		SystemUser user = (SystemUser)WebRequestUtils.retrieveSecureUser(request);
		Json j = new Json();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date date = sdf.parse(t.getRq());
		try {
			t.setPZLB("3");
			t.setJZRQ(date);
			t.setZD(user.getName());
			t.setZDRQ(new Timestamp(System.currentTimeMillis()));
			hZJZPZService.updateByPrimaryKeySelective(t);
			j.setSuccess(true);
			j.setMsg("修改成功");
			j.setObj(t);
		} catch (Exception e) {
			 e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}
	
	
	/**
	 * 跳转凭证编辑页面
	 * @return
	 */
	@RequestMapping("/editPage.shtml")
	public String editPage(String id ,HttpServletRequest request) {
		HZJZPZ hZJZPZ = this.hZJZPZService.selectByPrimaryKey(id);
		request.setAttribute("hZJZPZ", hZJZPZ);
		return "/business/accounting/zzpz/zzpzupdate";
	}
	
	/**
	 * 添加方法
	 * @param t
	 * @param session
	 * @param request
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping("/add.shtml")
	@ResponseBody
	public Json add(HZJZPZ t , HttpSession session,NativeWebRequest request) throws ParseException {
		SystemUser user = (SystemUser)WebRequestUtils.retrieveSecureUser(request);
		Json j = new Json();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date date = sdf.parse(t.getRq());
		try {
			t.setPZLB("3");
			t.setJZRQ(date);
			t.setZD(user.getName());
			t.setZDRQ(new Timestamp(System.currentTimeMillis()));
			hZJZPZService.insert(t);
			j.setSuccess(true);
			j.setMsg("添加成功");
			j.setObj(t);
		} catch (Exception e) {
			e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}
	
	/**
	 * 转账凭证删除
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete.shtml")
	@ResponseBody
	public Json delete(String id) {
		Json j = new Json();
		try {
			hZJZPZService.deleteByPrimaryKey(id);
			j.setSuccess(true);
			j.setMsg("删除成功");
		} catch (Exception e) {
			 e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}
	
	
	/**
	 * 初始化页面
	 * @return
	 */
	@RequestMapping("/manager.shtml")
	public String manager() {
		return "/business/accounting/zzpz/zzpz";
	}
	
	/**
	 * 记账凭证查询
	 * @param param
	 * @return
	 */
	@RequestMapping("/zzpzdataGrid.shtml")
	@ResponseBody
	public DataGrid zzpzdataGrid(@RequestParam  Map param) {
		return xYRJZServiceI.zzpzByGrid(param);
	}
	
	
	/**
	 * 跳转增加页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/addPage.shtml")
	public String addPage(HttpServletRequest request) {
		HZJZPZ hZJZPZ  = new HZJZPZ();
		request.setAttribute("hZJZPZ", hZJZPZ);
		return "/business/accounting/zzpz/zzpzAdd";
	}
	
}
